package com.tensquare.base.controller;


import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by WF on 2020/12/14 15:28
 */
@RestController
@RequestMapping("label")
@CrossOrigin
@RefreshScope
@Slf4j
public class LabelController {
    @Autowired
    private LabelService labelService;
    @Value("${ip}")
    private String ip;
    //1.添加label
    @PostMapping
    public Result add(@RequestBody Label label){
        try {
            labelService.add(label);
            return new Result(true, StatusCode.OK,"添加标签成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"添加标签失败！");
        }
    }

    //2.查询所有标签
    @GetMapping
    public Result findAll(){
        //2.1)查询所有标签
        List<Label> labeList = labelService.findAll();
        //2.2)返回
        return new Result(true,StatusCode.OK,"查询所有标签成功！",labeList);
    }

    //3.根据id查询标签
    @GetMapping("{labelId}")
    public Result findById(@PathVariable String labelId){
        //3.1)根据id查询标签
        Label label = labelService.findById(labelId);
        log.info("这个ip为：" + ip);
        log.info("查询出的结果-》{}",label);
        //3.2)返回
        return new Result(true,StatusCode.OK,"查询单个标签成功！",label);
    }

    //4.修改标签
    @PutMapping("{labelId}")
    public Result update(@PathVariable String labelId,@RequestBody Label label){
        //4.1)将标签id与标签对象绑定
        label.setId(labelId);
        //4.2)进行标签的修改
        labelService.update(label);
        //4.3)返回
        return new Result(true,StatusCode.OK,"修改标签成功！");
    }

    //5.删除标签
    @DeleteMapping("{labelId}")
    public Result delete(@PathVariable String labelId){
        //5.1)删除标签
        labelService.delete(labelId);
        //5.2)返回
        return new Result(true,StatusCode.OK,"删除标签成功！");
    }

    //6.进行标签的条件查询
    @PostMapping("search")
    public Result search(@RequestBody Label label){
        //6.1)根据条件查询标签集合
        List<Label> labels = labelService.search(label);
        //6.2)返回
        return new Result(true,StatusCode.OK,"查询标签成功！",labels);
    }

    //7.进行标签分页条件查询
    @PostMapping("search/{page}/{size}")
    public Result search(@RequestBody Label label,@PathVariable Integer page,@PathVariable Integer size){
        //7.1)根据条件查询标签集合
        PageResult<Label> pageResult = labelService.search(label,page,size);
        //7.2)返回
        return new Result(true,StatusCode.OK,"查询标签分页成功！",pageResult);
    }


}
